<?php
namespace V1;
class E_Order extends \ResourceClass
{
    public $isActive = true;
    public $remark = '获取订单详情,修改收货人,电话,地址,送货时间,配送状态,备注信息,接单，拒单,支付信息';

    public function get(array $req, array $post, &$result){
        try {
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }
            if(!array_key_exists('id',$req)||empty($req['id'])){
                return 'require resource id';
            }
            $customer_handler = new \Biz\UserHandler();
            $customer_cls = $customer_handler ->getById($req['userType'],$req['uid']);
            if(!is_object($customer_cls) && $customer_cls->getEnterpriseId() ==false){
                return 'data error';
            }
            $enterprise_id = $customer_cls ->getEnterpriseId();
            if (\Biz\UserHandler::checkAuth($req['userType'],$req['uid'],$enterprise_id,$req['id'].$req['t'],$req['k']) == false) {
                 return 'data illegal';
            }
            $tmp = array();
            $order_handler = new \Biz\OrderHandler();
            $order_cls = $order_handler->getOrderById((intval($req['id'])));
            $order_info['id'] = $order_cls->getId();
            $order_info['ordercode'] = $order_cls->getCode();
            $order_info['enterprise_id'] = $order_cls->getEnterpriseId();
            $order_info['enterprise_customer_id'] = $order_cls->getEnterpriseCustomerId();
            $order_info['enterprise_channel_id'] = $order_cls->getEnterpriseChannelId();
            $order_info['status'] = $order_cls->getStatus();
            $order_info['contact'] = $order_cls->getContact();
            $order_info['tel'] = $order_cls->getTelephone();
            $order_info['price'] = $order_cls->getPrice();
            $order_info['discount'] = $order_cls->getDiscount();
            $order_info['coupon'] = $order_cls->getConupon();
            $order_info['need_pay_price'] = $order_cls->getNeedPayPrice();
            $order_info['pay_status'] = $order_cls->getPayStatus();
            $order_info['pay_datetime'] = $order_cls->getPayTime();
            $order_info['pay_message'] = $order_cls->getPayMessage();
            $order_info['delivery_status'] = $order_cls->getDeliveryStatus();
            $order_info['delivery_charge'] = $order_cls->getDeliveryCharge();
            $order_info['delivery_time'] = $order_cls->getDeliveryTime();
            $order_info['delivery_province_id'] = $order_cls->getDeliveryProvinceId();
            $order_info['delivery_city_id'] = $order_cls->getDeliveryCityId();
            $order_info['delivery_address'] = $order_cls->getDeliveryAddress();
            $order_info['remark'] = $order_cls->getRemark();
            $order_info['active_discount'] = $order_cls->getActiveDiscount();
            $order_info['insert_time'] = $order_cls->getInsertTime();
            $order_info['update_time'] = $order_cls->getUpdateTime();
            $order_info['is_pay_type'] = $order_cls->getIsPayType();
            $order_info['pay_type_item_id'] = $order_cls->getPayTypeItemId();
            $order_info['transaction_id'] = $order_cls->getTransactionId();
            $order_info['status_string'] = $order_cls->getStatusStr();
            $order_info['pay_status_string'] = $order_cls->getPayStatusStr();
            $order_info['delivery_status_string'] = $order_cls->getDeliveryStatusStr();
            if(is_object($order_cls) && $order_cls->getEnterpriseCustomerInfo() != false){
                $order_info['customer_name'] = $order_cls->getEnterpriseCustomerInfo()->getContact();
                $order_info['customer_company'] = $order_cls->getEnterpriseCustomerInfo()->getCompany();
                $order_info['customer_tel'] = $order_cls->getEnterpriseCustomerInfo()->getTelePhone();
            }else{
                $order_info['customer_name'] = '未知';
            }
            $orderHeader = $this->orderFiled($order_info);
            $tmp['orderHeader'] = $orderHeader?$orderHeader:array();
            if((array_key_exists('is_sku',$req))&&($req['is_sku']==1)){
                $order_detail_handler = new \Biz\OrderDetailHandler();
                $order_detail_cls = $order_detail_handler->getOrderDetailByOrderId($order_info['id']);
                $orderBodySku = $this->skuFiled($order_detail_cls,$orderHeader);
                $tmp['orderBody'][] = $orderBodySku?$orderBodySku:array();
            }
            $result = $tmp;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

    public function put(array $req, array $post, &$result){
        try {
            if(!array_key_exists('id',$req)&&!empty($req['id'])){
                return 'require resource id';
            }
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }
            if (!array_key_exists('payload',$post)) {
                return 'require payload';
            }
            $data = base64_decode($post['payload']);
            @parse_str($data,$data);
            if(!array_key_exists('enterpriseId',$data)){
                return 'essential data required';
            }
            if (\Biz\UserHandler::checkAuth($req['userType'],$req['uid'],$data['enterpriseId'],$req['id'].$req['t'].$post['payload'],$req['k']) == false) {
                return 'data illegal';
            }
            if(!array_key_exists('actionType',$data)){
                return 'essential data required';
            }
            $order_handler = new \Biz\OrderHandler();
            switch($data['actionType']) {
                case "confirm":
                    $order_cls = $order_handler->getOrderHeadById($req['id']);
                    if ($order_cls->getEnterpriseId() == $data['enterpriseId']) {
                        $order_cls->setStatus(\Cls\Order\Order::ORDER_STATUS_CONFIRMED);
                        $update = $order_handler->update($order_cls);
                    }
                    if ($update === true) {
                        $solr_order_cls = $order_handler->getSolrOrderByOrderId($req['id']);
                        $solr_order_cls->setStatus(\Cls\Order\SolrOrder::ORDER_STATUS_CONFIRMED);
                        $solr_update = $order_handler->updateSolrOrder($solr_order_cls);
                    } else {
                        return 'confirm failed';
                    }
                break;
                case "refuse":
                    $order_cls = $order_handler->getOrderHeadById($req['id']);
                    if ($order_cls->getEnterpriseId() == $data['enterpriseId']) {
                        $order_cls->setStatus(\Cls\Order\Order::ORDER_STATUS_SHOP_CANCELED);
                        $update = $order_handler->update($order_cls);
                    }
                    if ($update === true) {
                        $solr_order_cls = $order_handler->getSolrOrderByOrderId($req['id']);
                        $solr_order_cls->setStatus(\Cls\Order\SolrOrder::ORDER_STATUS_SHOP_CANCELED);
                        $solr_update = $order_handler->updateSolrOrder($solr_order_cls);
                    } else {
                        return 'refuse failed';
                    }
                break;
                case "update":
                    $order_cls =  \Biz\Order\Order::getOrderById($req['id']);
                    if($data['enterpriseId'] == $order_cls->getEnterpriseId()){
                        $order_cls->setId($req['id']);
                        if(array_key_exists('tel',$data)){
                            $order_cls->setTelePhone($data['tel']);
                        }
                        if(array_key_exists('deliveryStatus',$data)){
                            $order_cls ->setDeliveryStatus($data['deliveryStatus']);
                        }
                        if(array_key_exists('deliveryTime',$data)){
                            $order_cls ->setDeliveryTime($data['deliveryTime']);
                        }
                        if(array_key_exists('remark',$data)){
                            $order_cls ->setRemark($data['remark']);
                        }
                        if(array_key_exists('deliveryAddress',$data)){
                            $order_cls ->setDeliveryAddress($data['deliveryAddress']);
                        }
                        $updateOrder = $order_handler->update($order_cls);
                        if($updateOrder === true ){
                            $solr_order_cls = $order_handler->getSolrOrderByOrderId($req['id']);
                            if(array_key_exists('deliveryStatus',$data)){
                                $solr_order_cls ->setDeliveryStatus($data['deliveryStatus']);
                            }
                            $solr_update = $order_handler->updateSolrOrder($solr_order_cls);
                        }else{
                            return 'update failed';
                        }
                    }
                break;
                case "paymentInfo":
                    $order_cls =  \Biz\Order\Order::getOrderById($req['id']);
                    if($data['enterpriseId'] == $order_cls->getEnterpriseId()){
                        $order_cls->setId($req['id']);
                        if(array_key_exists('payStatus',$data)){
                            $order_cls->setPayStatus($data['payStatus']);
                        }
                        if(array_key_exists('payMessage',$data)){
                            $order_cls ->setPayMessage($data['payMessage']);
                        }
                        $updateOrder = $order_handler->update($order_cls);
                        if($updateOrder === true ){
                            $solr_order_cls = $order_handler->getSolrOrderByOrderId($req['id']);
                            if(array_key_exists('payStatus',$data)){
                                $solr_order_cls ->setPayStatus($data['payStatus']);
                            }
                            $solr_update = $order_handler->updateSolrOrder($solr_order_cls);
                        }else{
                            return 'update failed';
                        }
                    }
                break;
            }
        } catch (Exception $e){
            return $e->getMessage();
        }
    }
    private static function trims($v){
        return trim($v);
    }

    private function orderFiled($result){
        $channel_id =$result['enterprise_channel_id'];
        $order_id =$result['id'];
        $enterprise_id = $result['enterprise_id'];
        $channel_cls = new \Biz\ChannelHandler();
        $channel_name = $channel_cls ->getChannelById($channel_id);
        if(is_object($channel_name) && $channel_name->getName() !=false){
            $result['channel_name'] = $channel_name->getName();
        }else{
            $result['channel_name'] = '未知';
        }
        $transaction_id = $result['transaction_id']?$result['transaction_id']:0;
        $transaction_handler = new \Biz\TransactionHandler();
        $payType = $transaction_handler->getTransactionById($transaction_id);
        if($payType && $transaction_id && ($result['pay_status']==\Cls\Order\Order::PAY_STATUS_HAD)){
            if($payType->getPlatform()=='WX'){
                $result['pay_class_type'] = '微信支付';
            }elseif($payType->getPlatform()=='ZFB'){
                $result['pay_class_type'] = '支付宝支付';
            }else{
                $result['pay_class_type'] = '线下支付';
            }
        }else if($transaction_id == 0 && $result['pay_status']==\Cls\Order\Order::PAY_STATUS_HAD){
            $result['pay_class_type'] = '线下支付';
        }else{
            $result['pay_class_type'] = '';
        }
        //判断是什么状态的订单
        $refund_order_handler = new \Biz\RefundOrderHandler();
        $refund_order_cls = $refund_order_handler->getByIdAndEnterpriseIdAndStatus($order_id,$enterprise_id);
        if($refund_order_cls === true){
            $result['type'] = '2';//退单
        }else{
            $result['type'] = '1';//订单
        }
        return $result;
    }

    private function skuFiled($result,$orderHeader){
        if($result&&$orderHeader){
            /**
             * @var \Cls\Order\Detail $product_cls
             */
            foreach($result as $product_cls) {
                $order_detail_tmp = array();
                $order_detail_tmp['id'] = $product_cls->getId();
                $order_detail_tmp['order_id'] = $product_cls->getOrderId();
                $order_detail_tmp['product_id'] = $product_cls->getProductId();
                $order_detail_tmp['product_name'] = $product_cls->getProductName();
                $order_detail_tmp['product_sku_id'] = $product_cls->getProductSkuId();
                $order_detail_tmp['product_sku_attr'] = $product_cls->getProductSkuAttr();
                $order_detail_tmp['individual_packing'] = $product_cls->getSellUnitString(); // 老版本
                $order_detail_tmp['mass_packing'] = $product_cls->getSellUnitString();// 老版本
                $order_detail_tmp['sell_unit_string'] = $product_cls->getSellUnitString();
                $order_detail_tmp['active_condition'] = $product_cls->getActiveCondition();
                $order_detail_tmp['active_num'] = $product_cls->getActiveNum();
                $order_detail_tmp['active_discount'] = $product_cls->getActiveDiscount();
                $order_detail_tmp['cost'] = $product_cls->getCost();
                $order_detail_tmp['price'] = $product_cls->getPrice();
                $order_detail_tmp['repertory_sum'] = $product_cls->getRepertorySum();
                $order_detail_tmp['amount'] = $product_cls->getAmount();
                $order_detail_tmp['total'] = $product_cls->getTotal();
                $order_detail_tmp['is_active'] = $product_cls->getActiveStatus();
                $order_detail_tmp['img'] = $product_cls->getProductInfo()->dealIconUrl();
                $result = $order_detail_tmp;
            }
            return $result;
        }
    }
}


?>